package main
import(
	"fmt"
	"github.com/garyburd/redigo/redis" //引入redis包
)

func main(){
	//通过go向redis 写入数据 和 读取数据
	//1.连接到redis
	c , err := redis.Dial("tcp","127.0.0.1:6379")
	if err != nil {
		fmt.Println("redis连接错误 err =",err)
		return
	}
	defer c.Close()
	_,er := c.Do("Set","test","1234")
	if er != nil {
		fmt.Println("set err =",er)
		return
	}
	r,err := redis.Int(c.Do("Get","test"))
	if err != nil{
		fmt.Println("get err=",err)
	}
	fmt.Println("get 成功 数据为：",r)

	//批量哈希插入
	_,err = c.Do("Hmset","user01","name","dddd","address","湖北省","age",28)
	if err != nil {
		fmt.Println("Hmset err=",err)
	}
	//批量哈希获取
	r1,err := redis.Strings(c.Do("Hmget","user01","name","age","address"))
	if err != nil {
		fmt.Println("Hmget err=",err)
	}
	//提取内容
	for k,v := range r1 {
		fmt.Printf("k = %v ,v = %v \n",k,v)
	}
}
